/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.recordsCenter.service.impl;

import me.zhengjie.modules.his.domain.ProjectType;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.recordsCenter.repository.ProjectTypeRepository;
import me.zhengjie.modules.his.recordsCenter.service.ProjectTypeService;
import me.zhengjie.modules.his.recordsCenter.service.dto.ProjectTypeDto;
import me.zhengjie.modules.his.recordsCenter.service.dto.ProjectTypeQueryCriteria;
import me.zhengjie.modules.his.recordsCenter.service.mapstruct.ProjectTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author his
* @date 2022-11-11
**/
@Service
@RequiredArgsConstructor
public class ProjectTypeServiceImpl implements ProjectTypeService {

    private final ProjectTypeRepository projectTypeRepository;
    private final ProjectTypeMapper projectTypeMapper;

    @Override
    public Map<String,Object> queryAll(ProjectTypeQueryCriteria criteria, Pageable pageable){
        Page<ProjectType> page = projectTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectTypeMapper::toDto));
    }

    @Override
    public List<ProjectTypeDto> queryAll(ProjectTypeQueryCriteria criteria){
        return projectTypeMapper.toDto(projectTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ProjectTypeDto findById(Integer projectTypeId) {
        ProjectType projectType = projectTypeRepository.findById(projectTypeId).orElseGet(ProjectType::new);
        ValidationUtil.isNull(projectType.getProjectTypeId(),"ProjectType","projectTypeId",projectTypeId);
        return projectTypeMapper.toDto(projectType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ProjectTypeDto create(ProjectType resources) {
        return projectTypeMapper.toDto(projectTypeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectType resources) {
        ProjectType projectType = projectTypeRepository.findById(resources.getProjectTypeId()).orElseGet(ProjectType::new);
        ValidationUtil.isNull( projectType.getProjectTypeId(),"ProjectType","id",resources.getProjectTypeId());
        projectType.copy(resources);
        projectTypeRepository.save(projectType);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer projectTypeId : ids) {
            projectTypeRepository.deleteById(projectTypeId);
        }
    }

    @Override
    public void download(List<ProjectTypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectTypeDto projectType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目类型", projectType.getProjectTypeName());
            map.put("创建者", projectType.getCreateBy());
            map.put("更新者", projectType.getUpdateBy());
            map.put("创建日期", projectType.getCreateTime());
            map.put("更新时间", projectType.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}